<template>
  <a-modal :open="open" width="950px" title="新增装备使用" centered :ok-button-props="{ disabled: !isCompleted }" ok-text="保存" @ok="onOk" @cancel="onCancel">
    <div class="flex">
      <div class="w-1/2 form-style-flex">
        <a-form :model="formState" class="form-style-flex">
          <a-form-item label="装备名称" name="equipmentName" class="w-full" v-bind="validateInfos.equipmentName">
            <SelectTable v-model:value="formState.equipmentName" type="配置列表" placeholder="请选择" custom-input empty-close @select="onSelect($event)" />
          </a-form-item>
          <a-form-item label="设备序列号" class="w-full">
            <a-input v-model:value="formState.equipmentNumber" placeholder="请选择" disabled />
          </a-form-item>
          <a-form-item label="品牌" class="w-full">
            <a-input v-model:value="formState.brand" placeholder="请选择" disabled />
          </a-form-item>
          <a-form-item label="规格" class="w-full">
            <a-input v-model:value="formState.specifications" placeholder="请选择" disabled />
          </a-form-item>
          <a-form-item label="型号" class="w-full">
            <a-input v-model:value="formState.model" placeholder="请选择" disabled />
          </a-form-item>
          <a-form-item label="单位" class="w-full">
            <a-input v-model:value="formState.unit" placeholder="请选择" disabled />
          </a-form-item>
          <a-form-item label="开始时间" class="w-full">
            <a-date-picker v-model:value="formState.startTime" format="YYYY-MM-DD" allow-clear value-format="YYYY-MM-DD" />
          </a-form-item>
          <a-form-item label="结束时间" class="w-full">
            <a-date-picker v-model:value="formState.endTime" format="YYYY-MM-DD" allow-clear value-format="YYYY-MM-DD" />
          </a-form-item>
          <a-form-item label="使用人" name="empName" class="w-full" v-bind="validateInfos.empName">
            <SelectTable v-model:value="formState.empName" type="医生" placeholder="请选择" custom-input empty-close @select="onSelectName($event)" />
          </a-form-item>
        </a-form>
      </div>
      <div class="w-1/2 pl-3">
        <template v-if="fileList.length > 0">
          <div class="flex flex-wrap max-h-[380px] overflow-auto">
            <div v-for="(item, index) in fileList" :key="index" class="w-[190px] h-[140px] mr-5 mb-2">
              <img alt="example" style="width: 100%; height: 100%; transition: transform 0.5s" :src="item.url" />
            </div>
          </div>
        </template>
        <template v-else>
          <a-empty />
        </template>
      </div>
    </div>
    <a-form>
      <a-form-item label="使用描述" class="w-full">
        <a-textarea v-model:value="formState.description" :rows="6" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import useStrore from '@/store/user.js'

const store = useStrore()
const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})

const fileList = ref([])
const formState = ref({
  equipmentName: '',
  empName: '',
  startTime: dayjs().format('YYYY-MM-DD'),
  endTime: dayjs().format('YYYY-MM-DD')
})
const rulesState = ref({
  equipmentName: [{ required: true, message: '请选择装备名称' }],
  empName: [{ required: true, message: '请选择医生' }]
})

// 装备
const onSelect = (row) => {
  formState.value.equipmentCode = row?.equipmentCode
  formState.value.equipmentName = row?.equipmentName
  formState.value.equipmentNumber = row?.equipmentNumber
  formState.value.brand = row?.brand
  formState.value.specifications = row?.specifications
  formState.value.model = row?.unit
  getImageList()
}

// 使用人
const onSelectName = (row) => {
  formState.value.empName = row?.empName
  formState.value.empId = row?.empId
}

// 图片
const getImageList = async () => {
  const res = await post('war/equipmentDistribution/getEquipmentImageList', { distributionId: formState.value.equipmentCode })
  if (res.code !== 0) return message.error(res.msg)
  fileList.value = res.data.map((item) => {
    return {
      ...item,
      uid: item.id,
      name: item.url,
      status: 'done',
      url: item.url
    }
  })
}

const { isCompleted, resetFields, validateInfos } = useForm(formState, rulesState)

const onOk = async () => {
  const { code, msg } = await post('/war/equipmentUsage/doSaveEquipmentUsage', formState.value)
  if (code !== 0) return message.error(msg)
  message.success(msg)
  emit('success')
  onCancel()
}

const onCancel = () => {
  emit('update:open', false)
  resetFields()
}

watch(
  () => props.open,
  (val) => {
    if (val) {
      formState.value = {
        startTime: dayjs().format('YYYY-MM-DD'),
        endTime: dayjs().format('YYYY-MM-DD'),
        ...props.rowData,
        empName: props.rowData?.empName || store?.empName,
        empId: props.rowData?.empId || store?.empId
      }
      if (formState.value?.equipmentImages && formState.value?.equipmentImages.length > 0) {
        fileList.value = formState.value?.equipmentImages.map((item) => {
          return {
            ...item,
            uid: item.id,
            name: item.url,
            status: 'done',
            url: item.url
          }
        })
      }
    } else {
      fileList.value = []
    }
  }
)
</script>
<style lang="less" scoped></style>
